package com.loop.homework;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Demo {
    public static void main(String[] args) {
        test();
    }

    /**
     * 1.请设计一个方法,能够验证客户输入的电话号码是否正确, 如果输入电话号码不正确,
     *
     * 按照如下要求将每一种异常情况抛出对应的异常信息; 如果输入电话号码正确, 将电话号码的4-7位使用*表示 (138****8866)
     *
     *    要求 :    1) 全数字
     *
     * 	           2)电话号码总长度11位
     *
     * 	           3)开头必须为1
     *
     * 	           4)第二位为3-9的数字
     */

    public static void isPhoneNum(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入手机号：");
        String next = scanner.next();
        if (next == null || "".equals(next)){
            System.out.println("请输入内容");
            isPhoneNum();
        }

        try {
            //将字符串去掉两边空格后转为char数组
            char[] chars = next.trim().toCharArray();
            if (chars.length !=11){
                throw new Exception("电话号码总长度11位");
            }else if (chars[0] != 1){
                throw new Exception("开头必须为1");
            }else if (! (chars[1] >50 && chars[1]<58) ){//ASCII编码表中3为51，9为57
                throw new Exception("第二位为3-9的数字");
            }
            Long.parseLong(next);
            StringBuilder stringBuilder = new StringBuilder(next);
            stringBuilder.replace(4,8,"****");
            System.out.println(stringBuilder.toString());
        } catch (NumberFormatException e) {
            System.out.println("请输入数字");
            isPhoneNum();
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }

    /**
     * 2.定义一个长度为5的int型数组arr，数组元素为1-99（范围包含1和99）之间的任意随机数,且保证5个数不重复
     */

    public static void setIntArr(){
        int[] arr = new int[5];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            int num = random.nextInt(5)+1;

            if (i == 0){
                arr[i] =num;
            }

            boolean flag = true;
            for (int j = 0; j < i; j++) {
                if (num == arr[j]){
                    flag = false;
                    i--;
                    break;
                }
            }
            if (flag){
                arr[i] = num;
            }

        }
        System.out.println(Arrays.toString(arr));
    }

    public static boolean isRepeat(int[] arr){
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            int b = 0;
            for (int j = 0; j < arr.length; j++) {
                if (arr[i]==arr[j]){
                    b++;
                }
                if (b==1){
                    arr[i] = random.nextInt(5)+1;
                    return false;
                }
            }
        }
        return true;
    }

    public static void test(){
        Random random = new Random();
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(5)+1;
        }
        while (!isRepeat(arr)){
            System.out.println(Arrays.toString(arr));
        }
    }



}
